/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */


//var dataID = $.getUrlVar("id");

var info = new Vue({
    el: "#app",
    data: {
        quesList: [],
        classify: [],
        isLoading: true,
        pageIndex: 1,
        pageSize: PAGESIZE,
        tabs: 0,
        reqOut: false, //判断请求数据是否已完
        addShow: false,
        screenSide: true,
        userSide: false,
        user: {},

        inputText: '',
        showModal: false,
        shareObj: {
            dataID: '',
            shareTit: '',
            sharePic: ''
        }
    },
    created: function() {
        var dtd = $.Deferred();
        var _self = this;
        /*
        //问题列表
        var data = {
            sortType: 1,
            pageSize: _self.pageSize,
            pageIndex: _self.pageIndex
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'ask_getQuestionList', datatype: 'json', type: 'get', data: data }, function(data) {
            if (data.error_code == 0) {
                _self.quesList = data.data;;
            } else {
                alert(data.msg);
            }
        });
        */
    },
    compiled: function() {
        var _self = this;
        //_self.userSideFn();
        $(document).scroll(function() {
            var docHeight = $(document).height(),
                winHeight = $(window).height();
            if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
                if (!_self.reqOut) {
                    _self.reqOut = true;
                    var str = '<img class="loading-img" src="images/loading.gif"></img>';
                    myLoad.pullLoad('quesCont', str);
                    info.loadlist(_self.tabs);
                }
            }
        });

        $(document).on('change.inputText', '.fillInput', function() {
            _self.inputText = $(this).val();
        });

        // 比例
        myJS.imgScale('.j_6', '6', '5');
        myJS.ImgFull('.loadImg', 'images/liby.png');

        //查看上次是否有Tab记录的先
        var backoperation = $.getCookie('backoperation');
        if (backoperation && typeof(backoperation) === 'string' && backoperation.length > 0) {
            $.setCookie('backoperation', "");
            //查看上次所保存的tab
            var _tabs = $.getCookie('askingtabs');
            if (_tabs) {
                //前三项有数据过滤的
                _self.tabs = parseInt(_tabs) > 3 ? 0 : parseInt(_tabs);
            }
        }

        _self.slides(_self.tabs);
    },
    /*ready:function(){
      this.loadPhoto();
    },*/
    methods: {
        loadlist: function(tabs) {
            var _self = this;
            var vModal, target; //用来储存不同的tab页面对应的对象
            switch (tabs) { //根据tab的切换来设置sortType的值
                case 0:
                    _self.screenSide = true;
                    var url = 'ask_getQuestionList';
                    var data = {
                        pageSize: _self.pageSize,
                        pageIndex: _self.pageIndex,
                        sortType: '1',
                        typeIDs: _self.typeID
                    }

                    vModal = _self;
                    target = _self.quesList;
                    break;
                case 1:
                    _self.screenSide = true;
                    var url = 'ask_getQuestionList';
                    var data = {
                        pageSize: _self.pageSize,
                        pageIndex: _self.pageIndex,
                        sortType: '2',
                        typeIDs: _self.typeID
                    };

                    vModal = _self;
                    target = _self.quesList;
                    break;
                default:

                    break;
            }

            if (!vModal.isLoading) return;
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: url, datatype: 'json', type: 'post', data: data }, function(data) {
                if (data.error_code == 0 && data.data.length > 0) {
                    if (vModal.pageIndex == 1) {
                        target.length = 0;
                    }
                    for (var i = 0; i < data.data.length; i++) {
                        target.push(data.data[i]);
                    }
                    _self.reqOut = false;

                    //实际数据与请求的数据量比较
                    if (data.data.length >= vModal.pageSize) {
                        myLoad.pullHide(); //加载后，把效果去掉
                    } else {
                        _self.reqOut = true;
                        myLoad.noDate(); //暂无数据
                        vModal.isLoading = false;
                    }
                } else {
                    _self.reqOut = true;
                    myLoad.noDate(); //暂无数据
                    vModal.isLoading = false;
                }

                vModal.pageIndex++;
            });

        },
        screen: function() {
            var _self = this;
            _self.screenSide = false;
            var url = 'ask_getTypeList';
            Common.ajaxCall({ url: url, datatype: 'json', type: 'post' }, function(data) {
                if (data.error_code == 0) {
                    _self.classify = data.data;

                } else {
                    alert(data.msg);
                }
            });
        },
        slides: function(index, e) {
            var _self = this;
            _self.tabs = index;

            if (e && typeof(e) != 'undefined') {
                $(e.currentTarget).addClass('on').siblings().removeClass('on');
            }
            
            //var str = '<img class="loading-img" src="images/loading.gif"></img>';
            //myLoad.pullLoad('quesCont', str);
            myLoad.pullHide();
            _self.quesList = []; //清空上一次请求vm储存的数据
            _self.reqOut = false;
            if (index === 2) { //调用分类接口
                _self.screen();
            } else { //调用问题列表
                var data = {
                    isLoading: true,
                    pageSize: PAGESIZE,
                    pageIndex: 1
                };
                _self = $.extend(_self, data);

                _self.loadlist(_self.tabs);
            }
            if (_self.tabs <= 3) {
                $.setCookie('askingtabs', _self.tabs); //保存最近的Tab
            }
        },
        loadPhoto: function() {
            // 比例
            myJS.imgScale('.j_3', '3', '1');
            myJS.imgScale('.j_6', '6', '5');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        add: function() {
            var _self = this;
            if (_self.addShow == false) {
                _self.addShow = true;
            } else {
                _self.addShow = false;
            }
        },
        userSideFn: function() {
            if (this.userSide == false) {
                this.userSide = true;
            } else {
                this.userSide = false;
            }
            var _self = this;
            // 顶部个人信息
            Common.ajaxCall({ url: "user_getUserInfo", datatype: 'json', type: 'post' }, function(data) {
                if (data.error_code == 0) {
                    _self.user = data.data;

                } else {
                    alert(data.msg);
                }
            });
        },
        swipeLeft: function(e) {
            var _self = this;
            if (_self.isEdit === '取消') return; //如果处于编辑状态则不予左滑

            var shareColl = $(e.target).parents('li');
            var touchDrift = ($(e.target).parents('li').width()) / 3;
            var drift = ($(e.target).parents('li').width()) / 3;
            //回复原位置
            _self.isDrift = false; //标识为非左滑状态
            $(e.target).parents('ul').children().animate({ left: 0 });
            $(e.target).parents('ul').children().find('.shareColl').animate({ width: 0, right: 0 });


            console.log(e.deltaX);
            if (e.deltaX < 0) { //左滑
                _self.isDrift = true; //标识为左滑状态
                if (e.deltaX < -touchDrift) {
                    $(e.target).parents('li').animate({ left: -drift });
                    var height = $(e.target).parents('li').outerHeight();
                    shareColl.find('.shareColl').animate({ width: drift, right: -drift }).show().css('line-height', height + 'px');
                }
            } else { //右滑
                if (e.deltaX > touchDrift) {
                    _self.isDrift = false; //标识为非左滑状态
                    $(e.target).parents('li').animate({ left: 0 });
                    shareColl.find('.shareColl').fadeOut();

                }
            }
        },
        share: function(obj, e) {
            $(e.target).parents('li').animate({ left: 0 }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var _self = this;
            _self.showModal = true;
            console.log(obj);
            _self.shareObj = {
                dataID: obj.shareId, //文档id
                shareTit: obj.shareTit, //文档标题
                sharePic: obj.sharePic,    //文档图片
                isdoc : obj.isdoc   //是不是文档
            };

        },
        shareReg: function(userList) {
            var _self = this;
            if (!userList) { //如果没选择分享对象回到原来界面
                _self.showModal = false;
                return;
            }

            alert({
                close: false,
                text: '分享',
                description: '<div class="clearfix shareTxt"><span class="j_1 photo"><img class="s_1" src="'+Common.wordIcon(_self.shareObj.sharePic,_self.shareObj.isdoc) +'" onerror=\"javascript:this.src=\'images/icon/viewIcon.png\';this.onerror=null;\"></span><span class="text">' + _self.shareObj.shareTit + '</span></div>',
                fillInput: true,
                kind: 2,
                okEvent: function() {
                    var data = {
                        dataID: _self.shareObj.dataID,
                        userList: userList,
                        comment: _self.inputText,
                        type: 3
                    };
                    data = Common.jsonEncode(data);
                    Common.ajaxCall({ url: 'look_share', datatype: 'json', type: 'get', data: data }, function(data) {
                        if (data.error_code == 0) {
                            var obj = data.data[0];
                            var code = obj.code;
                            var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                            alert({
                                text: '<img src="images/' + pic + '"/>',
                                description: obj.str,
                                kind: 0
                            });
                        } else {
                            alert(data.msg);
                        }
                    });
                }
            });
        },
        //收藏
        Collection: function(obj, e) {
            var _self = this;
            _self.addShow = false;
            $(e.target).parents('li').animate({ left: 0 }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var data = {
                dataID: obj.dataID,
                type: 3,
                isCollection: 1
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'look_addCollection', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: obj.str,
                        kind: 0
                    });
                } else {
                    alert(data.msg);
                }
            });
        },
        search: function() {
            window.location.href = "askSearch.html";
        }
    },
    watch: {
        "quesList": function() {
            this.loadPhoto();
        }
    }

});